﻿// 常量

var xmlhead = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
var get_detail  = 0;        // 是否已经获得用户资料
var get_friendlist  = 0;    // 是否已经获得好友列表
var imdebug         = 1;  
// 聊天窗口相关
function initdivmov () {

}
 
var x0=0,y0=0,x1=0,y1=0;
var offx=6,offy=6;
var moveable=false;
var hover='orange',normal='#336699';//color;
var index=10;//z-index;


var systemstoped = false;
 
 //  开始拖动
function startDrag()
{
	obj = this;
    if(event.button==1)
    {
        obj.setCapture();
        var win = obj.parentNode.parentNode;
        x0 = event.clientX;
        y0 = event.clientY;
        x1 = parseInt(win.style.left);
        y1 = parseInt(win.style.top);
        normal = obj.style.backgroundColor;

        moveable = true;
    }
}
//拖动函数
function drag()
{
	obj = this;

    if(moveable)
    {
        var win = obj.parentNode.parentNode;
        var sha = win.nextSibling;
        win.style.left = x1 + event.clientX - x0;
        win.style.top  = y1 + event.clientY - y0;
    }
}

//停止拖动
function stopDrag()
{
	obj = this;

    if(moveable)
    {
        var win = obj.parentNode.parentNode;
        win.style.borderColor     = normal;
        obj.style.backgroundColor = normal;
        obj.releaseCapture();
        moveable = false;
    }
}

//win div获得焦点
function getFocus()
{
	obj = this;
    if(obj.style.zIndex!=index)
    {
        index = index + 5;
        var idx = index;
        obj.style.zIndex=idx;
        //obj.children[1].children[1].children[1].zIndex=idx;
    }
}

//创建一个win对象;
function xWin(id)
{
	index = index+2;
	this.id      = id;
	this.obj     = null;
	this.bulid   = bulid;
	this.bulid(id);
}

//win对象初始化; 
function bulid(id)
{ 	
	var log = document.getElementById(this.id);	
	if (log)
	{			
		return;
	}

	/*	
          // 原型 可以在dom中观察
		<div id="msnwin + id " class="msnwin">
			<div id="msnhead+id" class="msnhead">
				<div id="headtitle +id" class="headtitle"></div>
				<div id="minbut +id" class="minbut"></div>
				<div id="closebut+ id" class="closebut"></div>	
			</div>
			
			<div id="msnmain+ id" class="msnmain">
				<div id="maintitle +id" class="maintitle"></div>
				<div id="chatmsg + id" class="chatmsg"></div>				
				<div id="myimg + id" class="myimg"> <img src="myimgsrc + id" width="100px" heigh ="100px"  /></div>
				<div id="frimg" + id" class="frimg"><img src="frimgsrc +id" width="100px" heigh ="100px" />   </div>
				<INPUT id="inputmsg" type="textarea" class="inputmsg">
				<div id="sendbut" + id class="sendbut"></div> 		
			</div>
		</div>
		
	*/
	var vmsnwin= document.createElement("div");

	vmsnwin.className = "msnwin";
	vmsnwin.id = "msnwin" + this.id;
	vmsnwin.object = this;	
	vmsnwin.style.zIndex = index;
	vmsnwin.onmousedown  = getFocus;
	vmsnwin.style.left = 120;
	vmsnwin.style.top  = 120;
	
		var vmsnhead= document.createElement("div");
		vmsnhead.className = "msnhead";
		vmsnhead.id = "msnhead" + this.id;

			var vheadtitle= document.createElement("div");
			vheadtitle.className	= "headtitle";
			vheadtitle.id			= "headtitle"+ this.id;

			vheadtitle.onmousedown=startDrag;
			vheadtitle.onmouseup=stopDrag;
			vheadtitle.onmousemove=drag;			
			vheadtitle.innerText = "正在和" + this.id + "聊天";
			//barwintitle.innerText += this.id;					
			//barwintitle.innerText +=" 聊天";			
	
			var vminbut= document.createElement("div");
			vminbut.className = "minbut";
			vminbut.onclick = minwin;	 	
			var vclosebut= document.createElement("div");
			vclosebut.className = "closebut";
			vclosebut.onclick = closewin; 
 			
		vmsnhead.appendChild(vheadtitle);	
		vmsnhead.appendChild(vminbut);
		vmsnhead.appendChild(vclosebut);		
		
		var vmsnmain= document.createElement("div");
		vmsnmain.className = "msnmain";
		vmsnmain.id = "msnmain" + this.id;		
				 			
			var vmaintitle= document.createElement("div");
			vmaintitle.className = "maintitle";
			vmaintitle.id = "maintitle" + id;
			
			var vchatmsg= document.createElement("div");
			vchatmsg.className = "chatmsg";
			vchatmsg.id = "chatmsg" + id;
			
			var vmyimg = document.createElement("div");
			vmyimg.className = "myimg";
			vmyimg.id = "myimg" + id;
			
			var vmyimgsrc = document.createElement("img");
			vmyimgsrc.id = "myimgsrc" + id;
			vmyimgsrc.src = myimgfileurl;
			vmyimgsrc.alt="您自己"
			vmyimgsrc.width = "100";
			vmyimgsrc.height = "100";
			
			var vfrimg= document.createElement("div");
			vfrimg.className = "frimg";
			vfrimg.id = "frimg" + id;
			
			
			
			var vfrimgsrc = document.createElement("img");
			vfrimgsrc.id = "frimgsrc" + id;
			vfrimgsrc.src = frimgfileurl;
			vfrimgsrc.alt="您的好友"
			vfrimgsrc.width = "100";
			vfrimgsrc.height = "100";
			
			
			var vinputmsg= document.createElement("input");
			vinputmsg.type ="text";
			vinputmsg.cols = 15;
			vinputmsg.rows =6;
			
			vinputmsg.id = "inputmsg" + id;
			//subbox1.size=24;
			vinputmsg.className = "inputmsg";		
			vinputmsg.onkeydown = inputmsgkeydown;
			
			var vsendbut= document.createElement("div");
			vsendbut.className = "sendbut";
			vsendbut.id = "sendbut" + id;	
			vsendbut.onclick = sendchatmsg;	


		vmyimg.appendChild(vmyimgsrc);
		vfrimg.appendChild(vfrimgsrc);
		
						
		vmsnmain.appendChild(vmaintitle);
		vmsnmain.appendChild(vchatmsg);			 
		vmsnmain.appendChild(vmyimg);			 
		vmsnmain.appendChild(vfrimg);

		
		vmsnmain.appendChild(vinputmsg);
		vmsnmain.appendChild(vsendbut);
			 
			
	vmsnwin.appendChild(vmsnhead);
	vmsnwin.appendChild(vmsnmain);	

	document.body.appendChild(vmsnwin);	

}

//win窗口最小化
function minwin()
{
	obj = this;
	var win = obj.parentNode.parentNode;
	var msghead = "msnwin";
	var id = win.id.substring(  msghead.length, win.id.length);
	var x = document.getElementById("msnmain" + id);
	if (x != null) {		
		if (x.style.display== "block") {
			x.style.display = "none";
		} else {
			x.style.display = "block";
		}		
	}	
		 
}

//win窗口销毁 
function closewin()
{
	obj = this;
	var win = obj.parentNode.parentNode; 
	win.style.display = "none";		 
}

// 返回从某个id (好友id)的聊天窗口中获取目前用户输入的数据
// 清空用户的输入数据，
function getchatmsg(id)
{
	var log = document.getElementById("inputmsg" + id);
	var x = "";	
	if (log) {
		 x = log.value;
		if (x.length) {		
			log.value = "";								
		}
	}	
	return x;
}

//win输入框keydown后激发 //发起者为 inputmsg +id 对象
function inputmsgkeydown()
{
	var code =event.keyCode;
	if (code==13) {
		var id = this.id;
		var msghead = "inputmsg";
		id = id.substring(msghead.length, id.length);		
		sendtochatserver(id);	
	}
}
//发送按键激发  //发起者为 sendbut +id 对象
function sendchatmsg()
{
	var id = this.id;
	var msghead = "sendbut";
	id = id.substring(msghead.length, id.length);	
	sendtochatserver(id);
}

// 将和某人输入框的消息发送送到服务器
function sendtochatserver(id)
{
	// 获取input 的消息
	var msgtxt = getchatmsg(id);	
	// 消息加入 消息框内
	var m1 = new Date();
	var msg1 = g_usernickname + " " + m1.toString() +": " + msgtxt;	
	addmsgtochatmsgdiv(id,msg1, "Serverchatmsgline");
	var xmlstr;
	
	xmlstr = xmlhead + "<root>";
	xmlstr += "<xmlcmd>" + makexml( userid,  id,  g_username,  1030, msgtxt) ;
	xmlstr += "</xmlcmd>";
	xmlstr += "</root>";		
	
	sendtowebserver(freshurl, xmlstr, processfreshServer);
	// 发送消息到服务器	

}

// 向用户id 里添加消息msg
function addmsgtochatmsgdiv( id, msg, msgclass)
{

	var x = document.getElementById("chatmsg" + id); 
		if (x !=  null) {
		// 向窗口添加信息
			while (x.getElementsByTagName("chatmsgline").length >= 50)
			{
				var t =x.getElementsByTagName("chatmsgline")[0];
				x.removeChild(t);							
			}
			
			var newel= document.createElement("chatmsgline");
			newel.nodeValue = msg;
			newel.innerText = decodeURIComponent(msg);
			newel.className = msgclass ;
			x.appendChild(newel);			
			x.scrollTop = x.scrollHeight;	
			//x.innerText +=decodeURIComponent( msg) + ;
		}	
}
//////////////////////////////////////////////////////////////////////////////////////////
// 密码相关
// 将一条消息进行rsa加密 并进行base64编码
function rsaEncrypt (msg)
{
  	return rsabase64encode(rsa.encrypt(msg)); 
}
// 初始化des密码
function initdes()
{
	var hashkey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	var index = 0;
	for (var x = 0 ; x < 8; x++)
	 {
		index =  parseInt(1000*Math.random() % (hashkey.length-3));
		deskey += hashkey.substr(index,1);
        
        index = parseInt( 1000*Math.random() % (hashkey.length-3));
		iv += hashkey.substr(index, 1) ;
	 }
}

//////////////////////////////////////////////////////////////////////////////////////////	

//主界面控制
// 显示或者消失一个div
function showdiv(divid, styles)
{		
	var x = document.getElementById(divid);
	x.style.display = styles;
}

function mouseondiv()
{
	this.style.backgroundColor = "#FF6600";
}
function mouseoutdiv()
{
	this.style.backgroundColor = "#ffffff";
}

/////////////////////////////////////////////////////////////////////////////////
// ajax操作
// 发送xml命令 原始api
function rawsendtowebserver(url , functionname) 
{
	// Open a connection to the server
	xmlHttp.open("POST",  url, true);
	// Setup a function for the server to run when it's done
	xmlHttp.onreadystatechange = functionname;
	// Send the request
	xmlHttp.send(null);
}

// 向webserver发送一个xml命令请求
function sendtowebserver(surl, xmlstr, processname)
{
	if (dessupport)
	{
		xmlstr = des (deskey, xmlstr, 1, 1, iv,1); 
	}
	
	xmlstr = base64encode(xmlstr);
	var m1 = new Date();
	var url = surl +"xmlfile=" + encodeURIComponent(xmlstr) + "&t1=" + encodeURIComponent(m1.toString());
	rawsendtowebserver(url , processname);		
}

/////////////////////////////////////////////////////////////////////////////////////////////
// 请求获取rsa public key
function getrsapublickey()
{	
	var xmlstr = xmlhead;
    xmlstr += "<root><xmlcmd></xmlcmd></root>";
	sendtowebserver(getrsaurl, xmlstr, processgetrsapublickey);	
}

// 获取rsa public key以后处理
function processgetrsapublickey()
{
	if (xmlHttp.readyState == 4)
	{
		try 
		{
			var strxml = xmlHttp.responseText;
			var xmldoc = new ActiveXObject("Microsoft.XMLDOM");		
			xmldoc.loadXML(strxml);	
			
			var nodeList = xmldoc.getElementsByTagName("Exponent");
			//alert(nodeList[0].firstChild.nodeValue);
			var Exponent = nodeList[0].firstChild.nodeValue;
			//var keyauth;
			//var username;
			nodeList = xmldoc.getElementsByTagName("Modulus");	
			//alert(nodeList[0].firstChild.nodeValue);
			var Modulus = 	nodeList[0].firstChild.nodeValue;
			//setInterval("freshServer()", 10000);
			rsa.setPublic(Modulus, Exponent );
			//createKey(Exponent, Modulus);  // 建立rsa 密钥	
            //启用loginbutt 控件
			var x = document.getElementById("loginbutton");
			//x.disabled = false ;
											
		}
		catch(e)
		{
			alert(strxml + "   " + e.message + "  密钥获取失败,请检查输入的消息和服务器的配置。重新尝试请刷新页面");
		}
	}
}
// 注册用户到服务
function logintoServer()
{
	// Build the URL to connect to	
	// 密钥格式
	// username
	// password
	// deskey
	var m1   = new Date();
	try{
		var url = loginurl;
		var x = document.getElementById("username");
		if (x.value.length == 0) { throw e }
		//用户名
		var afterrsa = x.value;
		if (rsasupport) {
			afterrsa = rsaEncrypt(x.value);
		}
		url += "username="+ encodeURIComponent(afterrsa);
		g_username =  x.value; 
		x = document.getElementById("passwd");
		if (x.value.length == 0) { throw e }
		afterrsa = x.value;
		if (rsasupport) {
			afterrsa = rsaEncrypt(x.value);
		}
		url  += "&passwd="+ encodeURIComponent(afterrsa);
        //密码
        url  += "&t1=" + encodeURIComponent(m1.toString());                             
        
        x = document.getElementById("ValidateCode");
		if (x.value.length == 0) { throw e } 
        //校验码
        url  += "&ValidateCode=" + encodeURIComponent(x.value);
        if (dessupport) {
        
        afterrsa = deskey;
		if (rsasupport) {
			afterrsa = rsaEncrypt(deskey);
		}           
        url  += "&deskey=" + encodeURIComponent(afterrsa);                           //des 的密码
        
        afterrsa = iv;
		if (rsasupport) {
			afterrsa = rsaEncrypt(iv);
		}  
		
        url  += "&iv=" + encodeURIComponent(afterrsa);                                   //des的初始化向量 
        }
        var y = document.getElementById("immainver1");
        x = document.getElementById("loginwindows");
        if (y && x) {
			y.removeChild(x);
        }
        
		rawsendtowebserver(url , processlogintoServer);
     } catch (e) {     
		alert(e);
     }  
     
	
}

//处理注册服务 
// 可以进一步抽象处理过程 将处理函数和结果放入
// 在以后版本优化	

var globalhtml =  "";

function processlogintoServer() 
{
	if (xmlHttp.readyState == 4)
	{
		try
		{				
				var strxml = xmlHttp.responseText;				
				strxml = base64decode(strxml);				
				//des 解码，以及去除解码带来的副作用数据。
				if (dessupport)
				{	
					strxml = des (deskey, strxml, 0, 1, iv,2);
				}
				var endstr = "</root>";
				var ind = strxml.indexOf(endstr);
				strxml = strxml.substr(0, ind+7);			
                globalhtml = strxml;
				var xmldoc = new ActiveXObject("Microsoft.XMLDOM");		
				xmldoc.loadXML(strxml);
                
                
				var xmlcmds = xmldoc.getElementsByTagName("xmlcmd");			
				var xmlcmd =  xmlcmds[0];		
				var nodeList = xmlcmd.getElementsByTagName("codes");

				var codes = nodeList[0].firstChild.nodeValue;
				if (codes != "2060") { throw e;}				
				//keyauth = nodeList[0].firstChild.nodeValue;
				//nodeList = xmlcmd.getElementsByTagName("username");	
				//username = 	nodeList[0].firstChild.nodeValue;
				//nodeList = xmlcmd.getElementsByTagName("userid");
				userid =   "1";				
				//showdiv("loginenum", "none");			
				//showdiv("loginingenum", "block");
				
				if (systemstoped == false)
				{
				    freshServer();
	    			freshtimer = setInterval("freshServer()", freshInterval);			    				
				}	
		}
		catch(e)
		{
			alert("login失败， 重新尝试请刷新页面!!");
		}
	}  
}

// 平时页面请求
function freshServer()
{ 
    if (systemstoped)
    {
        return;
    }
	var xmlstr;
	xmlstr = xmlhead;
	xmlstr += "<root><xmlcmd>"; 
 
	if (get_detail == 0) {
		xmlstr += makexml( userid, "MSNSERVER", g_username,  1070, "" );
	} else if (get_friendlist == 0) {
		xmlstr += makexml( userid, "MSNSERVER", g_username,  1010, "" );
		get_friendlist =1;
	} else {
		// 随机刷新 好友列表 或者 单纯刷新		
		xmlstr += makexml( userid, "MSNSERVER", g_username,  1000, "" );		
	}
	xmlstr += "</xmlcmd></root>";	
	sendtowebserver(freshurl, xmlstr, processfreshServer);
}

// 处理刷新后获取的信息
function processfreshServer(){
	if (xmlHttp.readyState == 4)
	{
		
		try {
			var strxml = xmlHttp.responseText;			
			strxml = base64decode(strxml);	
			
		    //des 解码，以及去除解码带来的副作用数据。
			if (dessupport)
			{	
				strxml = des (deskey, strxml, 0, 1, iv,2);
			}
			var endstr = "</root>";
			var ind = strxml.indexOf(endstr);
			strxml = strxml.substr(0, ind+7);			
			 globalhtml = strxml;
			var xmldoc = new ActiveXObject("Microsoft.XMLDOM");		
			//alert(strxml);
			xmldoc.loadXML(strxml);
			try 
			{
				processxmls(xmldoc);
			}
			catch(e) {
				alert("processxmls(xmldoc) error html = "   + globalhtml);			
			}
		
		}
		catch(e)
		{
            // 注销定时器
            freshtimer = null;
			alert("bad http result. im stoped");
		}
	}
}
//////////////////////////////////////////////////////////////////////////////
// 处理xml命令
function processxmls(xmldoc)
{ //xml消息处理
	var cmdlist = xmldoc.getElementsByTagName("xmlcmd");
	for (var i = 0; i < cmdlist.length; i++)
	{
		processxml(cmdlist[i]);	
	}	 
}

function processxml(xmldoc)
{ //xml消息处理

	var sender;
    var receive;
	var codes;
	var msg;
	var currtime;
	var  t;
    
	t =		xmldoc.getElementsByTagName("username");
	sender = t[0].text;    
    
    t = xmldoc.getElementsByTagName("receive");
    receive  = t[0].text;	
	if (receive != g_username) {
        return;
    }    
    t = xmldoc.getElementsByTagName("codes");
	codes = t[0].text;
	t = xmldoc.getElementsByTagName("msg");
	msg  = t[0].text;	
	t = xmldoc.getElementsByTagName("currtime");
	currtime  = t[0].text;

	var code  = parseInt(codes) %1000;
	code = (code  - (code  %10))/10;
    
    //alert (usename + " " + codes + "  " +  msg + "  " + currtime);
     
    if (imdebug) {
       // showlog(usename,codes, msg, currtime );	
    }
    // 很显然应该用多态来实现这个部分，但是javascript 我掌握得实在太弱了 :(
	switch (code ) {
	/*
                1000  刷新状态，  
                1010  请求用户列表
                1020  请求某用户的信息
                1030  发送聊天信息
                1040  退出系统
                1050  修改资料
                
                1090  错误
        */    
		case 0:
			process_fresh(sender, receive, codes, currtime, msg);		
			break;
					
		case 1:
			process_getfriendlist(sender, receive, codes, currtime, msg);
			break;
			
		
		case 2:
			process_getfrienddetail(sender, receive, codes, currtime, msg);
			break;
		
		case 3:
			process_chatmsg(sender, receive, codes, currtime, msg);
			break;
			
		case 4:
			process_quit(sender, receive, codes, currtime, msg);
			break;
	
		case 5:
			process_userdetail(sender, receive, codes, currtime, msg);
			break;
            
		case 6:		
			break;
			
		case 7:
			process_getuserdetail(sender, receive, codes, currtime, msg);
			break;
		case 9:
		    process_userwrong(sender, receive, codes, currtime, msg);
		    break;
		case 10:
		    process_friend_onoffline(sender, receive, codes, currtime, msg);
		    break;
		case 11:
			process_friend_detail(sender, receive, codes, currtime, msg);
			break;
		case 12:
			process_userimgurl(sender, receive, codes, currtime, msg);
			break;
		    	
	}	
}		



// 处理刷新返回结果
function process_fresh(sender, receive, codes, currtime, msg)
{

}
		
function process_getfriendlist(sender, receive, codes, currtime, msg)
{
	//
	//<freiend> <username> </username> <nickname> </nickname>							// <detail> </detail></freiend>
	try{

		var htmfriendpoint = document.getElementById("allfriends");
		msg = "<root>" + decodeURIComponent(msg) + "</root>";
		
		var xmldocs = new ActiveXObject("Microsoft.XMLDOM");			
		xmldocs.loadXML(msg);
		xmldocs = xmldocs.getElementsByTagName("root")[0];
		//friendlist = xmldocs.getElementsByTagName("friend");
		
		//var friendlist = htmfriendpoint.getElementsByTagName("friend");
		while ( htmfriendpoint.children.length )
		{
			var t =  htmfriendpoint.children[0];
			htmfriendpoint.removeChild(t);			
		}	
		
		var friendlist = xmldocs.getElementsByTagName("friend");
		
		for (var i = 0; i < friendlist.length; i++)
		{
			var tfrindnick = "";
			var tfrienddetail = "";
			try {
				;
				var tfrindname = base64decode(friendlist[i].getElementsByTagName("username")[0].text);
				try{
				
				if (friendlist[i].getElementsByTagName("nickname")[0].text.length) {
					tfrindnick = base64decode(friendlist[i].getElementsByTagName("nickname")[0].text);
					tfrindnick = decodeURIComponent(tfrindnick);
					tfrindnick = decodeURIComponent(tfrindnick);
                    }
                } catch (e) {
					alert("add error t2");
				}	
							
				try {
                    if (friendlist[i].getElementsByTagName("detail")[0].text.length) {
                        tfrienddetail = base64decode(friendlist[i].getElementsByTagName("detail")[0].text);
                        tfrienddetail = decodeURIComponent(tfrindnick);
                        tfrienddetail = decodeURIComponent(tfrindnick);
                    }
				}catch (e) {
					alert("add error ");
				}
					
				var divx =  document.createElement("div");
				divx.className		= "onefriendoffline";
				if (tfrindnick.length == 0) {
					divx.innerText = tfrindname;
				} else {
					divx.innerText  = tfrindnick ;
			    }
				divx.title	=  tfrindname + tfrindnick + tfrienddetail
				divx.id		= "friend" + tfrindname;                
                divx.name	="friend";
				//divx.onmouseover = mouseondiv;
				//divx.onmouseout  = mouseoutdiv;				
                divx.onclick     = createmsnwin;	
                
				htmfriendpoint.appendChild(divx);	 
				
				get_detail  = 1;
				get_friendlist  = 1;
			}			
			catch (e) {
				 alert("add friend error");			
			}
            //showdiv("loginingenum", "none");			
            //showdiv("userlist", "block");
            //showdiv("userdetail", "block");
		}
	}catch (e) {	
	}
}

/*				
function process_getfriendlist(xmldoc)
{
	//for (;;) {
		process_getfrienddetail();
	//}
}
*/

function process_getuserdetail(sender, receive, codes, currtime, msg)
{
	try {
		msg = decodeURIComponent(msg);
		msg = "<root>" + msg + "</root>";
		
		var xmldocs = new ActiveXObject("Microsoft.XMLDOM");			
		xmldocs.loadXML(msg);

		var t =	xmldocs.getElementsByTagName("nickname");
		
		g_usernickname  = decodeURIComponent(base64decode(t[0].text));
		t = xmldocs.getElementsByTagName("userdetail");
		g_nicknamedetail = decodeURIComponent(base64decode(t[0].text));
		t= document.getElementById("myusername");				
		t.innerText = g_username+ "  " + g_usernickname; //decodeURIComponent(base64decode(nickname));
		t= document.getElementById("mydetail");
		t.innerText = g_nicknamedetail; //+ "  " + decodeURIComponent(base64decode(nickname));
		//process_fresh(xmldoc);
		get_detail =1;
	}catch(e){
		alert(e);
	}
}

function process_getfrienddetail(sender, receive, codes, currtime, msg)
{
// 获取user name nick;	
	//var username = nick + "(" + name + ")";
	//addmsg('titlebar', 'usrname', username , null );
	
	try {
	/*
		var usename;
		var msg;
		var nickname;
		var currtime;
		
		var  t;
		t =		xmldoc.getElementsByTagName("sender");
		usename = t[0].text;	
		t = xmldoc.getElementsByTagName("nickname");
		nickname  = t[0].text;
		var x = document.getElementById("barwintitle" + usename); 
		// 获取chatbox mainwin
		if (x !=  null) {	
			x.innerText = "(" + usename +")" + decodeURIComponent(nickname);			}		
	*/	
	}catch (e) {
	
	}
}

function process_chatmsg(sender, receive, codes, currtime, msg)
{
	// 找到相应的msnwinwindows。加入数据	
	//addmsg( partwinid,  childwin, msg);		
	// 查找用户的chatwindow id
	
	try {

		var x = document.getElementById("msnwin" + sender); 
		// 获取msnwin
		if (x ==  null) {	
			var newwin = new xWin(sender);			
		}
		// msg 的格式 应为 发送人 发送时间 : \r\n
		//                                   消息内容

		
		var win = document.getElementById("msnwin" + sender);
		if (win != null) {
			win.style.display = "block";
		}
        
        msg = base64decode(msg);

        msg = decodeURIComponent(msg);
        var tempxmldom = new ActiveXObject("Microsoft.XMLDOM");			
		tempxmldom.loadXML(msg);
		


		var t  =	tempxmldom.getElementsByTagName("nickname");
		var t2 =	tempxmldom.getElementsByTagName("msnchatmag");
		if (!t2)
		{
			return;
		}
        if (t[0].text != "")
        {         
        	msg = t[0].text + " " + currtime +":" + t2[0].text; 
        }
        else
        {
        	msg = sender + " " + currtime +":" + t2[0].text;
        }
        NewchatMessageReceive();
        PlayCallSound(); // 播放声音
		addmsgtochatmsgdiv(sender, msg, "Localchatmsgline");
		
	}catch (e) {
	
	}
		
}

function process_quit(sender, receive, codes, currtime, msg)
{
    if (true ==systemstoped)
    {
        return;
    }
    systemstoped = true;
    clearInterval(freshtimer);
    freshtimer = null;
	alert(" 客户端已经断开了。请重新刷新页面后重新登录");	
}

function process_userwrong(sender, receive, codes, currtime, msg)
{
    if (true ==systemstoped)
    {
        return;
    }
    
    systemstoped = true;
    clearInterval(freshtimer);
    freshtimer = null;
	alert(" 客户端已经断开了。请重新刷新页面后重新登录");	

}


function process_friend_onoffline(sender, receive, codes, currtime, msg)
{
    	msg = decodeURIComponent(msg);
		msg = "<root>" + msg + "</root>";
		try{
		var xmldocs = new ActiveXObject("Microsoft.XMLDOM");			
		xmldocs.loadXML(msg);
		
		var t =	xmldocs.getElementsByTagName("friendname");
		
		t_friendname   = decodeURIComponent(base64decode(t[0].text));
		t = xmldocs.getElementsByTagName("friendstatus");
		t_friendstatus  = decodeURIComponent(base64decode(t[0].text));
		var t_win = document.getElementById("friend" + t_friendname);

 		if (t_friendstatus == "online")
		{
		    myMessageArray.push( t_win.title + " 改变状态上线了" ); 
		    t_win.className = "onefriendonline";            
		    return;
		}
		
		if (t_friendstatus == "offline")
		{
			myMessageArray.push( t_win.title + " 下线" ); 

		    t_win.className  = "onefriendoffline";
		    return;
		}
 				
    }
    catch( e)
    {
    
    }

}

function process_friend_detail(sender, receive, codes, currtime, msg)
{
		msg = decodeURIComponent(msg);
		msg = "<root>" + msg + "</root>";
		try{
		var xmldocs = new ActiveXObject("Microsoft.XMLDOM");			
		xmldocs.loadXML(msg);
		
		var t =	xmldocs.getElementsByTagName("friendname");
		var email = xmldocs.getElementsByTagName("email");
		var t1 = email[0].text;
		var t2 = base64decode(email[0].text);
		var t3 = decodeURIComponent(t2);
		t_friendname   = decodeURIComponent(base64decode(email[0].text));
		
		var v1 = xmldocs.getElementsByTagName("nickname");
		var v2 = v1[0].text;
		var v3  = base64decode(v1[0].text);
		var v4 = decodeURIComponent(v3);
		
		t_friendnickname  = decodeURIComponent(base64decode(v1[0].text));
		var t_win = document.getElementById("friend" + t_friendname);
	    t_win.innerText = t_friendnickname;            
	
	}catch ( e )
	{
	
	}
}

function process_userdetail(xmldoc)
{	
/*
	try {
		var detil = document.getElementById();		
	} catch(e) {	
	}
*/	
}


function process_userimgurl(sender, receive, codes, currtime, msg)
{
		msg = decodeURIComponent(msg);
		msg = decodeURIComponent(msg);
		msg = "<root>" + msg + "</root>";
		try{
		var xmldocs = new ActiveXObject("Microsoft.XMLDOM");			
		xmldocs.loadXML(msg);
		
		var username =	xmldocs.getElementsByTagName("username");
		var url = xmldocs.getElementsByTagName("url");
		
		var t1 = username[0].text;
		var t2 = base64decode(username[0].text);
		var t3 = decodeURIComponent(t2);
		 
		var v4 = url[0].text;
		var v5  = base64decode(url[0].text);
		var v6 = decodeURIComponent(v5);
		
	   		if (g_username == t3)
			{
				var x = document.getElementById("imgminurl");
				
				if (x)
				{			
					var turl = "." + v6 ;
					x.src =   turl; 
					var img = "myimg";
					myimgfileurl = turl;  // 修改本人头像				   
				}
			}
	    }
	    catch (e)
	    {
	    	alert(e);
	    
	    }  
	    



}

function createmsnwin()
{
	var friendid = this.id;
	var prekey = "friend";
	var friendname = friendid.substr(prekey.length , friendid.length);
	var x = document.getElementById("msnwin" + friendname); 
		// 获取chatbox mainwin
		if (x ==  null) {	
			var newwin = new xWin(friendname);
						
		}
	//var x = document.getElementById("msnwin" + friendname); 
	showdiv("msnwin" + friendname, "block");
	
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 生成xml命令
/*
 <xmlcmd>
 <userid> xxx</userid>
 <username>xxx </username>
 <codes>xxx</codes>
 <currtime>xxx</currtime>
 <msg> </msg>
 </xmlcmd>
*/
// 形成一个基本的xml命令，没有加上xmlcmd 头，是为了还有其他的扩充
function makexml( userid, receive  , username,  codes,  msg )
{
	var xml ="";
	xml += makexmlkey("userid",     encodeURIComponent(userid));
	xml += makexmlkey("receive",    encodeURIComponent(receive));
	xml += makexmlkey("username",   encodeURIComponent(username));
	xml += makexmlkey("codes",      codes.toString());
	xml += makexmlkey("msg",        encodeURIComponent(msg));
	var nowtime = new Date();
	xml += makexmlkey("currtime", nowtime.toString());
	return xml;
}

// 生成xml 的一个键值
function makexmlkey( k,  t)
{
	return "<" + k + ">" + t + "</" + k + ">";
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////
//debug 使用
function showlog(usename,codes, msg, currtime )
{
	var log = document.getElementById('logbox');	
	if (log ) 
	{
		var tags =(log).getElementsByTagName('div');
		if (tags) 
		{
			while (log.getElementsByTagName('div').length >= 150)
			{
                var x =log.getElementsByTagName("div")[0];
                log.removeChild(x);							
			}				
		} 
		
		var newel= document.createElement('div');
		var t1  = document.createElement('usename');
		t1.nodeValue = usename;
		t1.innerText = " " + usename + " ";
		var t2  = document.createElement('codes');
		t2.nodeValue = codes;
		t2.innerText = " "+ codes+ " ";
		var t3  = document.createElement('msg');
		t3.nodeValue = decodeURIComponent (msg);
		t3.innerText = decodeURIComponent (msg);
		var t4  = document.createElement('currtime');
		t4.nodeValue = currtime;
		t4.innerText = currtime;
		
		newel.appendChild(t1);
		newel.appendChild(t2);
		newel.appendChild(t3);
		newel.appendChild(t4);
		newel.className = "item";
		log.appendChild(newel);	
		//newel.appendChild(newtext);	
	}
}


function showchangendiv()
{
var x = document.getElementById('changenamediv');	
	if (x ) 
	{	
		x.style.display = "block";
	}

}

function changenickname()
{
	if (g_username == "")
	{
		alert("需要在msn成功登录系统后才能修改,请稍等");
		var x = document.getElementById('changenamediv');	
		if (x ) 
		{	
			x.style.display = "none";
		}
		return;
	}
	var nickname = document.getElementById('nickname');	
	if (nickname)
	{
		var xmlstr;
	
		xmlstr = xmlhead + "<root>";
		xmlstr += "<xmlcmd>" + makexml( userid,  "MSNSERVER",  g_username,  1100, nickname.value) ;
		xmlstr += "</xmlcmd>";
		xmlstr += "</root>";		
		sendtowebserver(freshurl, xmlstr, processfreshServer);
	
	}
	g_usernickname = nickname.value;
	var myusername = document.getElementById('myusername');	
	if (myusername)
	{
		 myusername.innerText = g_username+ "  " + g_usernickname; 
	}
	var x = document.getElementById('changenamediv');	
	if (x ) 
	{	
		x.style.display = "none";
	}

}


	
